package com.blog.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blog.service.PostService;
import com.blog.service.Impl.PostServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

@WebServlet("/Index.do")
public class IndexServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int pageNum = request.getParameter("pageNum") == null ? 1 : Integer.parseInt(request.getParameter("pageNum"));
		int pageSize = request.getParameter("pageSize") == null ? 6 : Integer.parseInt(request.getParameter("pageSize"));
		PostService ps =new PostServiceImpl();
		response.setContentType("application/json;charset=utf-8");
		new ObjectMapper().writeValue(response.getWriter(), ps.selectAllPost(pageNum, pageSize));
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
